package models.chat;

import org.hibernate.annotations.CollectionOfElements;
import play.db.jpa.Model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Konovalov_Nik
 */
@Entity
public class Channel extends Model {
    public String name;
    //@OneToMany(cascade = CascadeType.PERSIST)
    //public List<Message> messages;
    @ElementCollection
    public List<Long> memberIds =new ArrayList<Long>();

    public Channel(String name) {
        this.name = name;
        //this.messages = new ArrayList<Message>();
    }

    public List<Member> getMembers(){
        List<Member> members = new ArrayList<Member>(memberIds.size());
        for (Long memberId : memberIds) {
            members.add((Member) Member.findById(memberId));
        }
        return members;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Channel)) return false;

        return this.id.equals(((Channel) o).id);

    }

    @Override
    public int hashCode() {
        int result = 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (memberIds != null ? memberIds.hashCode() : 0);
        return result;
    }
}
